﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Collections.ObjectModel;

namespace MetMax
{
    [Serializable()]
    public class WrapAround : ICloneable
    {
        public string name { get; set; }
        public int wraps { get; set; }

        public WrapAround()
        {
        }

        public WrapAround(string name, int wraps)
        {
            this.name = name;
            this.wraps = wraps;
        }

        public WrapAround(WrapAround other)
            : this(other.name, other.wraps)
        {
        }

        #region ICloneable Members
        public object Clone()
        {
            return new WrapAround(this);
        }
        #endregion ICloneable Members
    }

    [Serializable()]
    public class WrapArounds : KeyedCollection<string, WrapAround>
    {
        public WrapArounds()
            : base()
        {
        }

        public WrapArounds(IEnumerable<WrapAround> items)
            : this()
        {
            foreach (WrapAround w in items)
            {
                this.Add(new WrapAround(w));
            }
        }

        public void WriteXml(Stream outStream)
        {
            DataTable dt = new DataTable();
            dt.TableName = "WrapArounds";
            dt.Namespace = @"http://bioinformatics.mpimp-golm.mpg.de/metmax/wraparounds";
            dt.Columns.Add(new DataColumn("name", typeof(string)));
            dt.Columns.Add(new DataColumn("wrapAround", typeof(int)));
            // Make the name column the primary key column.
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = dt.Columns["name"];
            dt.PrimaryKey = PrimaryKeyColumns;

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.DataSetName = "MetMax";
            ds.Namespace = @"http://bioinformatics.mpimp-golm.mpg.de/metmax";

            foreach (WrapAround w in this)
            {
                dt.Rows.Add(new object[] { w.name, w.wraps });
            }

            ds.AcceptChanges();
            ds.WriteXml(outStream);
            return;
        }

        public void ReadXml(Stream inStream)
        {
            this.Clear();

            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(inStream);

                DataTable dt = ds.Tables["WrapArounds"];
                foreach (DataRow dr in dt.Rows)
                {
                    this.Add(new WrapAround((string)dr[0], int.Parse((string)dr[1])));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, e.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        public new void Add(WrapAround newitem)
        {
            try
            {
                base.Add(newitem);
            }
            catch (ArgumentException argex)
            {
                MessageBox.Show(argex.Message);
            }
        }
        public new void Insert(int index, WrapAround newitem)
        {
            base.Insert(index, newitem);
        }
        public new void InsertItem(int index, WrapAround newitem)
        {
            base.InsertItem(index, newitem);
        }
        public new void ChangeItemKey(WrapAround item, string newkey)
        {
            base.ChangeItemKey(item, newkey);
        }


                    

        protected override string GetKeyForItem(WrapAround item)
        {
            return item.name;
        }
    }

}
